import {ColumnMappings, ColumnsExcludeType, DateFields, DateRangeFields} from "@/config/column";
import JSONPath from "jsonpath";
import XEUtils from "xe-utils";


const Utils = function () {
}


Utils.prototype.buildColumns = function (datumElement, columns) {
  columns = columns || []
  const _this = this;

  // console.log(datumElement)
  function getColumnWithFormData(datumElement) {
    const column_mappings = XEUtils.clone(ColumnMappings['common'], true)
    const type_column_mappings = ColumnMappings[datumElement['type']];
    if (type_column_mappings)
      Object.assign(column_mappings, type_column_mappings)
    const column = {}
    Object.keys(column_mappings).forEach(key => {
      const mapping = column_mappings[key]
      const value = mapping['selector'] ? JSONPath.query(datumElement, mapping['selector'], 1)[0] : mapping['default']
      if (value.iterable && XEUtils.isObject(value)) {
        _this.setJSONPathValue(value, datumElement)
        delete value['iterable']
      }
      JSONPath.value(column, key, value)
    })
    return column
  }
  // 此数据为卡片
  if (datumElement.widgetList && datumElement.widgetList.length > 0) {
    const widgetList = datumElement.widgetList
    widgetList.forEach((widget) => {
      return this.buildColumns(widget, columns)
    })
  }
  // 此数据为标签页时
  else if (datumElement.tabs && datumElement.tabs.length > 0) {
    const cols = datumElement.tabs
    cols.forEach((col) => {
      return this.buildColumns(col, columns)
    })
  }
  //此数据为表格时
  else if (datumElement.cols && datumElement.cols.length > 0) {
    const cols = datumElement.cols
    cols.forEach((col) => {
      return this.buildColumns(col, columns)
    })
  } else if (datumElement.type && !ColumnsExcludeType.includes(datumElement.type)) {
    // 此数据为展示字段时

    columns.push(getColumnWithFormData(datumElement))
  }
  return columns
}

Utils.prototype.setJSONPathValue = function (value, json) {
  const _this = this;
  if (XEUtils.isObject(value)) {
    XEUtils.each(value, (item, f) => {
      if (XEUtils.isObject(item)) {
        _this.setJSONPathValue(item, json)
      } else if (XEUtils.isString(item) && item.startsWith('\$.')) {
        item = item.split('|')
        value[f] = JSONPath.query(json, item[0], 1)[0]
        if (item.length === 2) {
          value[f] += item[1]
        }
      }
    })
  }
}

Utils.prototype.formatColumns = (columns) => {
  if (XEUtils.isEmpty(columns)) {
    return []
  }
  columns.forEach((col) => {
    // 日期格式转换
    if (utils.isDateRangeField(col)) {
      utils.format = col.formOptions ? !(XEUtils.isEmpty(col.formOptions.format)) ? col.formOptions.format : undefined : undefined;
      col.formatter = utils.dateFieldFormatter
    }
    // 时间查询
    else if (utils.isDateField(col)) {
      const format = col.formOptions ? !(XEUtils.isEmpty(col.formOptions.format)) ? col.formOptions.format : undefined : undefined;
      col.component = {name: 'date-format', props: {format: format.replace('yyyy', 'YYYY').replace('dd', 'DD')}}
    }
    // 筛选
    if (col.filter && col.formOptions.optionItems) {
      col.filters = col.formOptions.optionItems.map(item => ({text: item.label, value: item.value}))
      col.filterMethod = function (value, row) {
        const filterValue = row[col['key']];
        if (_.isArray(filterValue)){
          return filterValue.includes(value)
        }else{
          return filterValue === value
        }
      }
      col.filterMultiple = col.filterMultiple || false
    }
    if (col.search) {
      col.search.disabled = !col.search.disabled
    }
  })
  return columns;
}

Utils.prototype.isDateRangeField = (col) => {
  return DateRangeFields.includes(col.type)
}

Utils.prototype.isDateField = (col) => {
  return DateFields.includes(col.type)
}

Utils.prototype.is_dynamic = (menu) => {
  return !menu.is_datalog && menu.is_dynamic
}


Utils.prototype.dateFieldFormatter = (row, column, value, index) => {
  const format = utils.format;
  if (value != null && value instanceof Array && value.length > 1) {
    if (_.isEmpty(value)) {
      return undefined
    }
    return doFormat(value[0], format) + '至' + doFormat(value[1], format)
  }
  return doFormat(value, format)
}

function doFormat(value, format) {
  if (_.isEmpty(value)) {
    return undefined
  }
  const date = dayjs(value)
  if (!date.isValid())
    return value
  return date.format(format)
}

export const utils = new Utils()
